<?php

require_once('admincontroller.php');

class Emails extends AdminController
{
	function Emails()
	{
		parent::AdminController();
		$this->load->model('User');
		$this->load->model('UserStatus');
		$this->load->model('EmailMessage');
		$this->main_navigation = 'candidats';
		$this->sub_navigation = 'emails';
		$this->data = array();
	}
	
	function index()
	{
		$data['emails'] = $this->EmailMessage->all();
		$this->display('email_index', $data);
	}
	
	function create()
	{
		$data['email'] = new EmailMessage();
		$data['statuses'] = $this->UserStatus->all();
		$data['selected_statuses'] = array();
		$this->display('email_edit', $data);
	}
	
	function edit($id)
	{
		$data['email'] = $this->EmailMessage->get_by_id($id);
		$data['statuses'] = $this->UserStatus->all();
		$data['selected_statuses'] = array();
		$this->display('email_edit', $data);
	}
	
	function save($id = 0)
	{
		$email = null;
		if ($id == 0) {
			$email = new EmailMessage();
			$email->creator_id = $this->session->userdata('user_id');
		}
		else {
			$email = $this->EmailMessage->plain_get($id);
		}
		$email->updater_id = $this->session->userdata('user_id');
		$email->updated_at = date_db_format();
		$errors = $this->EmailMessage->update($email, $_POST);
		if ($_POST['email_submit'] == 'save_and_send') {
			$email->sent_at = date_db_format();
		}
		if (count($errors) == 0 && $this->EmailMessage->save($email)) {

			if ($_POST['email_submit'] == 'save_and_send' && isset($_POST['user_status'])) {
				// extraction des ids des statuts
				$status_filter = 'u.status_id IN ('.implode(', ', array_map('intval', $_POST['user_status'])).')';
				$users = $this->User->all($status_filter);
				// sélection des destinataires
				$adresses = array();
				foreach ($users as $user) {
					$adresses[] = $user->email;
				}
				$this->load->library('email');
				$this->email->from(CONTACT_EMAIL);
				$this->email->to(CONTACT_EMAIL);
				$this->email->bcc($adresses);
				$this->email->subject($email->subject);
				$this->email->message($email->body);
				if ($this->email->send()) {
					$data['success_notice'] = 'Message enregistré et envoyé à '.count($adresses).' destinataire(s)';
				}
				else {
					$data['warning_notice'] = "Le message a été enregistré mais l'envoi a échoué";
				}
				$data['emails'] = $this->EmailMessage->all();
				
				$this->display('email_index', $data);
			}
			else {
				$data['emails'] = $this->EmailMessage->all();
				$data['success_notice'] = 'Message enregistré';
				$this->display('email_index', $data);
			}
			
		}
		else {
			$data['email'] = $email;
			$data['error_notice'] = $errors;
			$data['statuses'] = $this->UserStatus->all();
			$data['selected_statuses'] = isset($_POST['user_status']) ? $_POST['user_status'] : array();
			$this->display('email_edit', $data);
		}
	}
	
	function details($id)
	{
		$data['email'] = $this->EmailMessage->get_by_id($id);
		$this->load->view('admin/emails/email_details', $data);
	}
	
	function delete($id)
	{
		if (!$this->EmailMessage->delete($id)) {
			$this->output->set_status_header(400);
		}		
	}
}


